package com.obas.integration.element.retriever;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ease.common.log.LogDeclaration;
import com.ease.common.vo.editor.AbstractObjectEditor;
import com.ease.integration.telent.CommonsNetTelnetClient;
import com.ease.integration.telent.TelnetClient;
import com.ease.integration.telent.TelnetExecutionCallback;
import com.ease.integration.telent.TelnetTemplate;
import com.obas.integration.element.loader.DeviceMacAddressLoader;
import com.obas.model.business.DeviceMacAddress;

public abstract class TelnetDeviceMacAddressLoader extends TelnetTemplate implements DeviceMacAddressLoader {

	private static final transient Log log = LogFactory.getLog(LogDeclaration.class);

	protected String equipmentModelCode;

	/** 实例数据 */
	protected String host;
	protected int port;
	protected String username;
	protected String password;

	/** 规格数据 */
	protected String passwordPrompt;
	protected String usernamePrompt;
	protected String command;
	protected String morePrompt;

	public abstract AbstractObjectEditor<DeviceMacAddress> getRawEditor();

	public abstract List<DeviceMacAddress> editTextAsObject(String rawDeviceMacAddress);

	public List<DeviceMacAddress> loadDeviceMacAddress() {

		String rawDeviceMacAddress = this.loadRawDeviceMacAddress();

		// String rawDeviceMacAddress =
		// FileUtil.readClassPathResourceToString(HWDefaultTelnetDeviceMacAddressLoader.class, this.equipmentModelCode
		// + ".txt");

		System.out.println(rawDeviceMacAddress);

		log.debug(rawDeviceMacAddress);

		return editTextAsObject(rawDeviceMacAddress);
	}

	public String loadRawDeviceMacAddress() {
		org.apache.commons.net.telnet.TelnetClient targetTelnetClient = new org.apache.commons.net.telnet.TelnetClient();

		CommonsNetTelnetClient telnetClient = new CommonsNetTelnetClient();
		telnetClient.setTargetTelnetClient(targetTelnetClient);

		telnetClient.setHost(host);
		telnetClient.setPort(port);
		telnetClient.setUsername(username);
		telnetClient.setPassword(password);

		telnetClient.setPasswordPrompt(passwordPrompt);
		telnetClient.setUsernamePrompt(usernamePrompt);
		telnetClient.setMorePrompt(morePrompt);

		this.setTelnetClient(telnetClient);

		return doLoadRawDeviceMacAddress();
	}

	public String doLoadRawDeviceMacAddress() {
		return this.execute(new TelnetExecutionCallback<String>() {
			public String execute(TelnetClient telnetClient) {
				telnetClient.request(command);
				return telnetClient.response(null);
			}
		});
	}

	/**
	 * @return the morePrompt
	 */
	public String getMorePrompt() {
		return morePrompt;
	}

	/**
	 * @param morePrompt
	 *            the morePrompt to set
	 */
	public void setMorePrompt(String morePrompt) {
		this.morePrompt = morePrompt;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @param host
	 *            the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * @return the usernamePrompt
	 */
	public String getUsernamePrompt() {
		return usernamePrompt;
	}

	/**
	 * @param usernamePrompt
	 *            the usernamePrompt to set
	 */
	public void setUsernamePrompt(String usernamePrompt) {
		this.usernamePrompt = usernamePrompt;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the passwordPrompt
	 */
	public String getPasswordPrompt() {
		return passwordPrompt;
	}

	/**
	 * @param passwordPrompt
	 *            the passwordPrompt to set
	 */
	public void setPasswordPrompt(String passwordPrompt) {
		this.passwordPrompt = passwordPrompt;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the command
	 */
	public String getCommand() {
		return command;
	}

	/**
	 * @param command
	 *            the command to set
	 */
	public void setCommand(String command) {
		this.command = command;
	}

	/**
	 * @return the equipmentModelCode
	 */
	public String getEquipmentModelCode() {
		return equipmentModelCode;
	}

	/**
	 * @param equipmentModelCode
	 *            the equipmentModelCode to set
	 */
	public void setEquipmentModelCode(String equipmentModelCode) {
		this.equipmentModelCode = equipmentModelCode;
	}

}
